{include file="Public:header2"/}
<body style="min-height: 100%;background-color: #fff">

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form onsubmit="return false;">
                        <div class="layui-form" wid100 lay-filter="">
                            <div class="layui-form-item">
                                <label class="layui-form-label">类型</label>
                                <div class="layui-input-inline" style="width: 600px">
                                    <input type="radio" name="type" value="1" lay-filter="type" class="layui-input" title="首页" lay-filter="target" checked>
                                    <input type="radio" name="type" value="3" lay-filter="type" class="layui-input" title="商城" lay-filter="target" >
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">排序值</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="sort" value="0" lay-verify="required" placeholder="请输入排序值" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            
                            <span class="jump_type" style="display:block">   
                            <div class="layui-form-item">
                                <label class="layui-form-label">跳转类型</label>
                                <div class="layui-input-inline" style="width: 600px">
                                    <input type="radio" name="ttype" value="0" lay-filter="ttype" class="layui-input" title="不跳转" checked>
                                    <input type="radio" name="ttype" value="1" lay-filter="ttype" class="layui-input" title="优惠券" >
                                    <input type="radio" name="ttype" value="2" lay-filter="ttype" class="layui-input" title="一番赏(包含全局赏,积分赏)" >
                                    <input type="radio" name="ttype" value="3" lay-filter="ttype" class="layui-input" title="无限赏(包含领主赏)" >
                                    <input type="radio" name="ttype" value="4" lay-filter="ttype" class="layui-input" title="连击赏" >
                                </div>
                            </div>
                            </span>
                            
                            <div class="layui-form-item coupon_id">
                                <label class="layui-form-label">标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" value="" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item coupon_id">
                                <label class="layui-form-label">优惠券id</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="coupon_id" value="0" lay-verify="required" placeholder="请输入优惠券id" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item goods_id">
                                <label class="layui-form-label">盒子id</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="goods_id" value="0" lay-verify="required" placeholder="请输入盒子id" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">图片</label>
                                <div class="layui-input-inline">
                                    <input type="hidden" name="imgurl" value="" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input" >
                                    <img style="float:left;width:260px;height: 160px" class="" id="imgurl" src="/static/image/upload.jpg">
                                </div>
<!--                                <div class="layui-form-mid layui-word-aux">提示：图片尺寸比例为：750:216，建议最大不超过1M，格式为：.jpg，.jpeg，.png</div>-->
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" onclick="return check()">确认保存</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="Public:footer"/}
</body>
</html>

<script type="text/javascript">
    layui.use(['layer','form','upload','element'], function(){
        var $ = layui.$;
        var upload = layui.upload;
        var element = layui.element;
        var form = layui.form;

        $(".coupon_id").hide();
        $(".goods_id").hide();
        form.on('radio(ttype)', function (data) {
            var value = data.value;   //  当前选中的value值
            if(value == 1){
                $(".coupon_id").show();
                $(".goods_id").hide();
            }else if(value == 2) {
                $(".coupon_id").hide();
                $(".goods_id").show();
            }else if(value == 3) {
                $(".coupon_id").hide();
                $(".goods_id").show();
            }else if(value == 4) {
                $(".coupon_id").hide();
                $(".goods_id").show();
            }else {
                $(".coupon_id").hide();
                $(".goods_id").hide();
            }
        });

        //执行实例
        var uploadInst = upload.render({
            accept:'imgurl'
            ,elem: '#imgurl' //绑定元素
            ,url: '/admin/picture' //上传接口
            ,done: function(res){
                //上传完毕回调
                if(res.status == 1){ //成功
                    layer.msg("上传成功",{icon:1,time:1000},function(){
                        $("#imgurl").attr("src",res.data.path);  //单图
                        $("input[name=imgurl]").val(res.data.imgurl);
                    });
                }else{ //失败
                    layer.msg(res.msg,{icon:2,anim:6,time:1500});
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('服务繁忙，请稍后再试',{icon:2,anim:6,time:1500});
            }
        });
        
        
        //选择是否发货
        form.on('radio(type)', function(data){
          if(data.value == 1){
            $('.jump_type').show();
          }else{
            $('.jump_type').hide();
            $("input[name=ttype]").val('');
          }
        });
        
        
    });

    function check(){
        var url="{:url('/admin/advert_add')}";
        var $ = layui.$;
        var load=layer.load(2);
        $.post(url,$("form").serialize(),function(data){
            if(data.status==1){
                layer.msg(data.msg,{icon:1,time:1500},function(){
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                    parent.location.reload();
                });
            }else{
                layer.msg(data.msg,{icon:2,anim:6,time:1500},function(){
                    layer.close(load);
                });
            }
        })
    }
</script>